using System;
using System.Drawing;
using Microsoft.DirectX.Direct3D;

namespace SharpWind.Gui.Controls
{
	public abstract class CheckBoxBase : ButtonBase
	{
		protected bool isChecked;

		public event EventHandler CheckedChanged = null;

		public CheckBoxBase( string control ) : base( control )
		{

		}

		internal override void Render()
		{
			if( Visible )
			{
				Engine.Instance.Renderer.GuiSprite.Begin( SpriteFlags.AlphaBlend );

				Theme theme = Engine.Instance.Scene.Controls.Theme;
				Theme.Control checkBox = theme.Controls[this.control];
				Theme.Control.Element checkmark = checkBox.Elements["Mark"];
				Theme.Control.Element element = checkBox.Elements[this.curentState.ToString()];

				Engine.Instance.Renderer.GuiSprite.Draw2D( theme.Texture.D3DTexture,
				                                           new Rectangle( element.X, element.Y, element.Width, element.Height ),
				                                           new SizeF( Size.Height, Size.Height ),
				                                           new PointF( Location.X, Location.Y ), element.Color );

				if( Checked )
				{
					Engine.Instance.Renderer.GuiSprite.Draw2D( theme.Texture.D3DTexture,
					                                           new Rectangle( checkmark.X, checkmark.Y, checkmark.Width,
					                                                          checkmark.Height ), new SizeF( Size.Height, Size.Height ),
					                                           new PointF( Location.X, Location.Y ), checkmark.Color );
				}

				Engine.Instance.Renderer.GuiSprite.End();
			}
		}

		protected override void OnMouseDown()
		{
			Checked = !Checked;
		}

		public virtual bool Checked
		{
			get
			{
				return this.isChecked;
			}
			set
			{
				if( this.isChecked != value )
				{
					this.isChecked = value;
					if( CheckedChanged != null )
					{
						CheckedChanged( this, EventArgs.Empty );
					}
				}
			}
		}

		public override PointF Location
		{
			get
			{
				return this.location;
			}
			set
			{
				this.location = value;
			}
		}

		public override SizeF Size
		{
			get
			{
				return this.size;
			}
			set
			{
				this.size = value;
			}

		}
	}
}
